package com.jbp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.model.Skill;
import com.jbp.model.SkillApplyReq;
import com.jbp.model.SkillParam;
import com.jbp.model.SkillUser;
import com.jbp.service.SkillParamService;
import com.jbp.service.SkillService;
import com.jbp.service.SkillUserService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 技能参数(SkillParam)表控制层
 * @author cc
 * @since 2020-11-19 17:06:55
 */
@Api(tags ="APP接口--SkillParam" )
@RestController
@RequestMapping("rest/skillParam")
public class SkillParamController {
    @Autowired
    private SkillParamService skillParamService;
    @Autowired
    private SkillService skillService;
    @Autowired
    private SkillUserService skillUserService;

    /** 申请技能 参数列表 */
    @ApiOperation(value="申请技能 参数列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Skill.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/applyList")
    public R applyList(@RequestParam Integer skillId){
        Skill skill = skillService.getById(skillId);
        List<SkillParam> list =  skillParamService.applyList(skillId);
        skill.setParams(list);
        return R.ok(skill);
    }


    /** 申请 */
    @ApiOperation(value="申请技能")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Skill.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @PostMapping("/apply")
    public R apply(@RequestBody SkillApplyReq req, @RequestAttribute("userId") Integer userId){
        Skill skill = skillService.getById(req.getSkillId());
        if(skill==null) return R.error("技能不存在");
        if(skill.getStatus()==0) return R.error("技能已关闭");

        SkillUser skillUser = skillUserService.getOne(
                new QueryWrapper<SkillUser>().eq("user_id", userId)
                .eq("skill_id", req.getSkillId())
        );

        if (skillUser!=null) {
            if (skillUser.getStatus()==2) return R.error("技能审核中");
            if (skillUser.getStatus()==3) return R.error("已经通过认证了");
        }

        skillUserService.apply(userId, req);

        return R.ok();
    }
}